{% extends "base.html" %}
{% block title %}{% endblock %}
{% block content %}
<br>
<form class="layui-form" action="" id="cross_srh" lay-filter="cross_srh">
    <div class="proxy_srh">
        <div class="layui-inline">
            <div class="layui-inline">

                <div class="layui-input-inline">
                    <label class="layui-form-label">任务类型*：</label>
                    <div class="layui-input-inline">
                        <select class="layui-select" name="work_type" id="work_type" lay-verify="" lay-search
                                lay-filter="work_type">
                            <option value=''>请选择</option>
                            <option value='0'>用例设计</option>
                            <option value='1'>用例执行</option>
                            <option value='2'>测试开发</option>
                            <option value='3'>测试总结</option>
                            <option value='4'>评审相关</option>
                            <option value='5'>方案设计</option>
                            <option value='6'>其它工作</option>
                        </select>
                    </div>
                </div>
                <div class="layui-input-inline">
                    <label class="layui-form-label">责任人*：</label>
                    <div class="layui-input-inline">
                        <select class="layui-select" name="person" id="person" lay-verify="" lay-search
                                lay-filter="work_charge">
                            <option value=''>请选择</option>
                            {% for d in userAll %}
                                <option value="{{d.id}}">{{d.userName}}</option>
                            {% endfor %}

                        </select>
                    </div>
                </div>

                <br><br>
                <div class="layui-input-inline" id="work_content_div">
                    <label class="layui-form-label">任务内容*：</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="work_content" id="work_content"
                               placeholder="请输入"
                               style="width:298%">
                    </div>
                </div>


                <div class="layui-input-inline" id="case_version_div">
                    <label class="layui-form-label">用例版本*：</label>
                    <div class="layui-input-inline">
                        <select class="layui-select" name="case_version" id="case_version" lay-verify="" lay-search
                                lay-filter="case_version">
                        </select>
                    </div>
                </div>

                 <div class="layui-input-inline" id="case_module_div">
                    <label class="layui-form-label">用例模块*：</label>
                    <div class="layui-input-inline">
                        <div  id="case_module" style="width: 212px;"></div>
                        <!--<select class="layui-select" name="case_module" id="case_module" lay-verify="" lay-search-->
                                <!--lay-filter="case_module">-->
                        <!--</select>-->
                    </div>
                </div>
                <br><br>
                <div class="layui-input-inline">
                    <label class="layui-form-label">计划结束*：</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="p_end" id="p_end" placeholder="请输入"
                               style="width:212px">
                    </div>
                </div>
                <div class="layui-input-inline">
                    <label class="layui-form-label">实际结束：</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="c_end" id="c_end" placeholder="请输入"
                               style="width:212px">
                    </div>
                </div>

                <div class="layui-input-inline" style="display:none">
                    <label class="layui-form-label">：</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="case_plan_id" id="case_plan_id" placeholder="请输入"
                               style="width:212px">
                    </div>
                </div>
                <div class="layui-input-inline" style="display:none">
                    <label class="layui-form-label">：</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="project_id" id="project_id" placeholder="请输入"
                               style="width:212px">
                    </div>
                </div>
                <div class="layui-input-inline" style="display:none">
                    <label class="layui-form-label">：</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="id" id="id" placeholder="请输入"
                               style="width:212px">
                    </div>
                </div>

                <br><br>
                <div class="layui-input-inline" style="left:20px">
                    <span id="redTips" style="color:red">此时为新增状态，即会插入一条新数据!</span>
                </div>
                <div class="layui-inline" style="float:right;">
                    <button class="layui-btn" type="button" id="start" lay-submit lay-filter="start">保存所有</button>
                </div>

            </div>
        </div>

    </div>

</form>

<script>
    layui.use(['table','element','xmSelect'], function(){
          var table = layui.table
          ,form = layui.form;
          var $= layui.jquery;
          var xmSelect = layui.xmSelect;
          var element = layui.element;

          $("#case_version_div").hide();
          $("#case_module_div").hide();

          //从缓存中取值，用来控制更新、保存、或者修改
          CacheSubPlan = layui.data('test').CacheSubPlan;
          NewCacheSubPlan = layui.data('test').NewCacheSubPlan;

          if(NewCacheSubPlan != undefined){
             form.val('cross_srh',{
                 project_id:NewCacheSubPlan.project_id
                 ,case_plan_id:NewCacheSubPlan.case_plan_id
                 ,p_end:NewCacheSubPlan.p_end
                 ,c_end:NewCacheSubPlan.c_end
             });
          }

          if(CacheSubPlan == undefined){

          }else{
              if(CacheSubPlan.data.work_type == "1"){
                  work_content = "";
                  case_version = CacheSubPlan.data.work_content.case_plan;
                  case_module = CacheSubPlan.data.work_content.case_module;
              }else{
                  work_content = CacheSubPlan.data.work_content.work_content;
                  case_version = "";
                  case_module = "";
              }
              form.val('cross_srh',{
                 project_id:CacheSubPlan.project_id
                 ,work_type:CacheSubPlan.data.work_type
                 ,person:CacheSubPlan.data.person
                 ,p_end:CacheSubPlan.data.p_end
                 ,work_content:work_content
                 ,case_version:case_version
                 ,c_end:CacheSubPlan.data.c_end
                 ,case_plan_id:CacheSubPlan.case_plan_id
                 ,id:CacheSubPlan.data.id
              });


              form.render();
              if(CacheSubPlan.data.work_type == 1){
                    $("#case_version_div").show();
                    $("#case_module_div").show();
                    $("#work_content_div").hide();
                    //发送请求，查询版本
                    project_id = $('#project_id').val();
                     //查询用例版本
                    $.ajax({
                         //发出请求
                        type:"GET",
                        url:"/basecase/casepaln/all?",
                        contentType: 'application/json; charset=utf-8',
                        dataType:"json",
                        data:{"project_id":project_id},
                        success: function(result){
                            s1 = '<div class="layui-input-inline" id="case_version_div"><label class="layui-form-label">用例版本*：</label><div class="layui-input-inline">'+
                                    '<select class="layui-select" name="case_version" id="case_version" lay-verify="" lay-search lay-filter="case_version">';
                            e1 ='</select></div></div>';
                            if(result.data.length>0){
                                str = '';
                                layui.each(result.data,function(k,v){
                                        plan = v.case_plan;
                                        str = str+'<option value="'+plan+'">'+plan+'</option>'
                                });
                                htmlDiv = s1+str+e1;
                                $("#case_version_div").html(htmlDiv);

                             }else{
                                $("#case_version_div").html(s1+e1);
                             }
                             $.ajax({
                                 //发出请求
                                type:"GET",
                                url:"/basecase/caseMainMod/all?",
                                contentType: 'application/json; charset=utf-8',
                                dataType:"json",
                                data:{"project_id":project_id,"case_plan":$('#case_version option:selected').val()},
                                success: function(result){
                                    var sarray = new Array();
                                    layui.each(result.data,function(k,v){
                                        blSlt = false;
                                        //去缓存里读取相关的值
                                        if(case_module!=""){
                                            case_module_list = case_module.split(',');
                                            layui.each(case_module_list,function(i,data){
                                                if(data == v.case_main_mod){
                                                    blSlt=true;
                                                }
                                            });
                                        }
                                        sarray.push({"name":v.case_main_mod,"value":v.case_main_mod, selected: blSlt});
                                    });
                                    var demo1 = xmSelect.render({
                                        el: '#case_module',
                                        data: sarray
                                    });

                                }
                            });
                            form.render();
                       }
                     });

              }
          }
          if($("#id").val()!=""){
             $("#redTips").html('<span id="redTips" style="color:red;left:20px">此时为更新状态!</span>');
          }

          form.on('submit(start)', function(data){
                if(NewCacheSubPlan!=undefined){
                    if(data.field.p_end > NewCacheSubPlan.p_end){layer.msg("计划结束时间不能超过主任务的时间");return false;}
                    if(data.field.c_end!="" &&NewCacheSubPlan.c_end!="" && NewCacheSubPlan.c_end!=undefined){
                        if(data.field.c_end > NewCacheSubPlan.c_end){layer.msg("实际结束时间不能超过主任务的时间");return false;}
                    }
                }
                if(CacheSubPlan!=undefined){
                    if(data.field.p_end > CacheSubPlan.data.p_end){layer.msg("计划结束时间不能超过主任务的时间");return false;}
                    if(data.field.c_end!="" &&CacheSubPlan.c_end!=""&&CacheSubPlan.c_end!=undefined){
                        if(data.field.c_end > CacheSubPlan.data.c_end){layer.msg("实际结束时间不能超过主任务的时间");return false;}
                    }
                }

                if(data.field.person == ""){layer.msg("责任人需要填写!");return false;}
                if(data.field.work_type == "1"){
                    work_content = {"case_plan":data.field.case_version,"case_module":data.field.select};
                }else{
                    if(data.field.work_content == ""){
                        layer.msg("细划的工作内容需要填写!");
                        return false;
                    }
                    work_content = {"work_content":data.field.work_content}
                }
                case_plan_id = data.field.case_plan_id

                content = {"id":data.field.id,"work_content":work_content,"work_type":data.field.work_type,"person":data.field.person,"p_end":data.field.p_end,"c_end":data.field.c_end,"case_plan_id":case_plan_id};
                //发送ajax保存请求
                $.ajax({
                         //发出请求
                        type:"POST",
                        url:"/casePlan/sub/manage",
                        contentType: 'application/json; charset=utf-8',
                        dataType:"json",
                        data:JSON.stringify(content),
                        success: function(result){
                            if(result.id){
                                layer.msg("保存成功");
                            }
                        }
                });
          });


          //根据用例版本查询模块
          form.on('select(case_version)', function(obj){
                project_id = $('#project_id').val();
                $.ajax({
                             //发出请求
                            type:"GET",
                            url:"/basecase/caseMainMod/all?",
                            contentType: 'application/json; charset=utf-8',
                            dataType:"json",
                            data:{"project_id":project_id,"case_plan":obj.value},
                            success: function(result){
                                var sarray = new Array();
                                layui.each(result.data,function(k,v){
                                    blSlt = false;
                                    //去缓存里读取相关的值
                                    if(case_module!=""){
                                        case_module_list = case_module.split(',');
                                        layui.each(case_module_list,function(i,data){
                                            if(data == v.case_main_mod){
                                                blSlt=true;
                                            }
                                        });
                                    }
                                    sarray.push({"name":v.case_main_mod,"value":v.case_main_mod, selected: blSlt});
                                });
                                var demo1 = xmSelect.render({
                                    el: '#case_module',
                                    data: sarray
                                });
                                $("#case_version_div").show();
                                $("#case_module_div").show();
                            }
                });


          });
          //监听下拉框
          form.on('select(work_type)', function(obj){
                  if(obj.value=="1"){

                     $("#work_content_div").hide();
                     project_id = $('#project_id').val();
                     //查询用例版本
                     $.ajax({
                         //发出请求
                        type:"GET",
                        url:"/basecase/casepaln/all?",
                        contentType: 'application/json; charset=utf-8',
                        dataType:"json",
                        data:{"project_id":project_id},
                        success: function(result){
                            s1 = '<div class="layui-input-inline" id="case_version_div"><label class="layui-form-label">用例版本*：</label><div class="layui-input-inline">'+
                                    '<select class="layui-select" name="case_version" id="case_version" lay-verify="" lay-search lay-filter="case_version">';
                            e1 ='</select></div></div>';
                            if(result.data.length>0){
                                str = '';
                                layui.each(result.data,function(k,v){
                                        plan = v.case_plan;
                                        str = str+'<option value="'+plan+'">'+plan+'</option>'
                                });
                                htmlDiv = s1+str+e1;
                                $("#case_version_div").html(htmlDiv);

                             }else{
                                $("#case_version_div").html(s1+e1);
                             }
                             $.ajax({
                                 //发出请求
                                type:"GET",
                                url:"/basecase/caseMainMod/all?",
                                contentType: 'application/json; charset=utf-8',
                                dataType:"json",
                                data:{"project_id":project_id,"case_plan":$('#case_version option:selected').val()},
                                success: function(result){
                                    var sarray = new Array();
                                    layui.each(result.data,function(k,v){
                                        sarray.push({"name":v.case_main_mod,"value":v.case_main_mod});
                                    });
                                    var demo1 = xmSelect.render({
                                        el: '#case_module',
                                        data: sarray
                                    });

                                }
                            });
                            form.render();
                            $("#case_version_div").show();
                            $("#case_module_div").show();
                       }
                    });
                    //查询模块


                  }else{
                     $("#case_version_div").hide();
                     $("#case_module_div").hide();
                     $("#work_content_div").show();
                  }

                  form.render('select');
          });

          form.render();
    });
</script>

<script>
//时间控件
layui.use('laydate', function(){
  var laydate = layui.laydate;
  laydate.render({
    elem: '#p_end',
    type:'date',
    format:'yyyy-MM-dd',
    trigger: 'click'
  });
  laydate.render({
    elem: '#c_end',
    type:'date',
    format:'yyyy-MM-dd',
    trigger: 'click'
  });

});
</script>
{% endblock %}